﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using DTO;
using BUS;

namespace QuanLyCongDoanVien.UserControls
{
    public partial class ucCapNhatThongTinCDV : UserControl
    {
        public ucCapNhatThongTinCDV()
        {
            InitializeComponent();
        }

        List<ChucVuDTO> lstChucVu = new List<ChucVuDTO>();
        bool blnNgayCDen = false;
        bool blnNgayCDi = false;
        bool blnChucVu = false;

        private bool kiemTraLaChuoiSo(string chuoiso)
        {
            int lenth = chuoiso.Length;
            int count = 0;
            string kitu = chuoiso.Substring(count, 1);
            int chuso;
            while (Int32.TryParse(kitu, out chuso) == true)
            {
                kitu = chuoiso.Substring(count, 1);
                count++;
                if (count == lenth)
                {
                    return true;
                }
            }

            return false;
        }

        private string KiemTraHopLe()
        {
            double hesoluong;
            int ngach;
            string chuoi_loi = "";
            if (txtMaCDV.Text.Trim() == "")
            {
                chuoi_loi += "Phải nhập vào mã công đoàn viên";
            }
            if (txtHoTen.Text.Trim() == "")
            {
                chuoi_loi += "Phải nhập vào họ tên công đoàn viên\n";
            }
            if (double.TryParse(txtHeSoLuong.Text, out hesoluong) == false)
            {
                chuoi_loi += "Hệ số lương phải là số thực\n";
            }
            if (int.TryParse(txtNgach.Text, out ngach) == false)
            {
                chuoi_loi += "Ngạch phải là số nguyên\n";
            }
            if (txtSoDienThoai.Text.Trim() != "" && kiemTraLaChuoiSo(txtSoDienThoai.Text.Trim()) == false)
            {
                chuoi_loi += "số điện thoại phải là chuỗi số\n";
            }
            if (txtSoTheDV.Text.Trim() != "" && kiemTraLaChuoiSo(txtSoTheDV.Text.Trim()) == false)
            {
                chuoi_loi += "số thẻ đoàn viên phải là chuỗi số\n";
            }
            return chuoi_loi;
        }

        private void ucCapNhatThongTinCDV_Load(object sender, EventArgs e)
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl.Name == "txtNhapTenCDV" || ctrl.Name == "cboChonToCongDoan" || (ctrl.Name.Contains("lbl") == true))
                {
                    ctrl.Enabled = true;
                }
                else
                {
                    ctrl.Enabled = false;
                }
               
                if (ctrl.Name.Contains("cboNgay"))
                {
                    ctrl.Text = "--Ngày--";
                }
                if (ctrl.Name.Contains("cboThang"))
                {
                    ctrl.Text = "--Tháng--";
                }
            }
            cboToCongDoan.DataSource = ToCongDoanBUS.LayDanhSach();
            cboToCongDoan.DisplayMember = "TenToCongDoan";
            cboToCongDoan.SelectedIndex = -1;

            cboChonToCongDoan.DataSource = ToCongDoanBUS.LayDanhSach();
            cboChonToCongDoan.DisplayMember = "TenToCongDoan";
            cboChonToCongDoan.SelectedIndex = -1;


            cboDanToc.DataSource = DanTocBUS.LayDanhSach();
            cboDanToc.DisplayMember = "TenDanToc";
            cboDanToc.SelectedIndex = -1;

            cboTonGiao.DataSource = TonGiaoBUS.LayDanhSach();
            cboTonGiao.DisplayMember = "TenTonGiao";
            cboTonGiao.SelectedIndex = -1;

            List<ChucVuDTO> lstCVu;
            lstCVu = ChucVuBUS.LayDanhSach();
            for (int i = 0; i < lstCVu.Count; i++)
            {
                chklstChucVu.Items.Add(lstCVu[i].TenChucVu);
            }
        }

        private void txtNhapTenCDV_TextChanged(object sender, EventArgs e)
        {
            if (txtNhapTenCDV.Text.Trim() != "" || cboChonToCongDoan.Text.Trim() != "")
            {
                btnDongY.Enabled = true;
            }
            else
            {
                btnDongY.Enabled = false;
            }
        }

        private void cboChonToCongDoan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtNhapTenCDV.Text.Trim() != "" || cboChonToCongDoan.Text.Trim() != "")
            {
                btnDongY.Enabled = true;
            }
            else
            {
                btnDongY.Enabled = false;
            }
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            dtgDanhSachCDV.Enabled = true;

            string strHoTen = txtNhapTenCDV.Text.Trim();
            ToCongDoanDTO toCDDto = new ToCongDoanDTO();
            if(cboChonToCongDoan.Text.Trim() != "")
            {
                toCDDto = (ToCongDoanDTO)cboChonToCongDoan.SelectedItem;
            }

            try
            {
                dtgDanhSachCDV.DataSource = CongDoanVienBUS.TimKiem("", strHoTen, toCDDto);
                dtgDanhSachCDV.Columns["tocongdoanid"].Visible = false;
                dtgDanhSachCDV.Columns["tocongdoan"].Visible = false;
                dtgDanhSachCDV.Columns["tongiao"].Visible = false;
                dtgDanhSachCDV.Columns["tongiaoid"].Visible = false;
                dtgDanhSachCDV.Columns["dantoc"].Visible = false;
                dtgDanhSachCDV.Columns["dantocid"].Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dtgDanhSachCDV_DoubleClick(object sender, EventArgs e)
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl.Name == "txtNhapTenCDV" || ctrl.Name == "cboChonToCongDoan" ||
                     ctrl.Name == "btnDongY")
                {
                    ctrl.Enabled = false;
                }
                else
                {
                    ctrl.Enabled = true;
                }
            }
            ResetThongTinCDV();

            string maCDV = dtgDanhSachCDV.CurrentRow.Cells["MaCDV"].Value.ToString();

            NgayChuyenDenDTO ngCDenDto = NgayChuyenDenBUS.TimKiem(maCDV);
            NgayChuyenDiDTO ngCDiDto = NgayChuyenDiBUS.TimKiem(maCDV);
            List<int> lstMaChucVu = CongDoanVienBUS.TimKiemChucVu(maCDV);

            txtMaCDV.Text = maCDV;
            txtHoTen.Text = dtgDanhSachCDV.CurrentRow.Cells["HoTen"].Value.ToString();
            txtDiaChi.Text = dtgDanhSachCDV.CurrentRow.Cells["DiaChi"].Value.ToString();

            if (String.Equals(dtgDanhSachCDV.CurrentRow.Cells["LaDangVien"].Value.ToString(), "True") == true)
                chkLaDangVien.Checked = true;
            else
                chkLaDangVien.Checked = false;

            cboGioiTinh.Text = dtgDanhSachCDV.CurrentRow.Cells["GioiTinh"].Value.ToString();
            cboTinhTrang.Text = dtgDanhSachCDV.CurrentRow.Cells["TinhTrang"].Value.ToString();
            cboTrinhDoVanHoa.Text = dtgDanhSachCDV.CurrentRow.Cells["TrinhDoVanHoa"].Value.ToString();
            cboLyLuanChinhTri.Text = dtgDanhSachCDV.CurrentRow.Cells["LyLuanChinhTri"].Value.ToString();
            cboChuyenMonNgVu.Text = dtgDanhSachCDV.CurrentRow.Cells["ChuyenMonNghiepVu"].Value.ToString();

            cboDanToc.SelectedIndex = Int32.Parse(dtgDanhSachCDV.CurrentRow.Cells["DanToc"].Value.ToString()) - 1;
            cboTonGiao.SelectedIndex = Int32.Parse(dtgDanhSachCDV.CurrentRow.Cells["TonGiao"].Value.ToString()) - 1;
            cboToCongDoan.SelectedIndex = Int32.Parse(dtgDanhSachCDV.CurrentRow.Cells["ToCongDoan"].Value.ToString()) - 1;

            double hesoluong = Double.Parse(dtgDanhSachCDV.CurrentRow.Cells["HeSoLuong"].Value.ToString());
            txtHeSoLuong.Text = hesoluong.ToString();

            txtNgach.Text = dtgDanhSachCDV.CurrentRow.Cells["Ngach"].Value.ToString();
            txtSoDienThoai.Text = dtgDanhSachCDV.CurrentRow.Cells["SoDT"].Value.ToString();
            txtSoTheDV.Text = dtgDanhSachCDV.CurrentRow.Cells["SoTheDoanVien"].Value.ToString();

            DateTime ngaysinh = DateTime.Parse(dtgDanhSachCDV.CurrentRow.Cells["NTNS"].Value.ToString());
            txtNamSinh.Text = ngaysinh.Year.ToString();
            cboNgaySinh.Text = ngaysinh.Day.ToString();
            cboThangSinh.Text = ngaysinh.Month.ToString();

            DateTime ngayVaoCD = DateTime.Parse(dtgDanhSachCDV.CurrentRow.Cells["NgayVaoCD"].Value.ToString());
            txtNamVaoCD.Text = ngayVaoCD.Year.ToString();
            cboThangVaoCD.Text = ngayVaoCD.Month.ToString();
            cboNgayVaoCD.Text = ngayVaoCD.Day.ToString();

            if (ngCDenDto.MaCDV != "")
            {
                blnNgayCDen = true;
                cboNgayCDen.Text = ngCDenDto.NgayChuyenDen.Day.ToString();
                cboThangCDen.Text = ngCDenDto.NgayChuyenDen.Month.ToString();
                txtNamCDen.Text = ngCDenDto.NgayChuyenDen.Year.ToString();
                txtLyDoChuyenDen.Text = ngCDenDto.LyDoChuyenDen.ToString();
            }

            if (ngCDiDto.MaCDV != "")
            {
                blnNgayCDi = true;
                cboNgayCDi.Text = ngCDiDto.NgayChuyenDi.Day.ToString();
                cboThangCDi.Text = ngCDiDto.NgayChuyenDi.Month.ToString();
                txtNamCDi.Text = ngCDiDto.NgayChuyenDi.Year.ToString();
                txtLyDoChuyenDi.Text = ngCDiDto.LyDoChuyenDi.ToString();
            }

            foreach (int i in lstMaChucVu)
            {
                blnChucVu = true;
                chklstChucVu.SetItemChecked(i - 1, true);
            }
        }
        private void ResetThongTinCDV()
        {
            blnChucVu = false;
            blnNgayCDen = false;
            blnNgayCDi = false;

            foreach (Control ctrl in grpThongTinCDV.Controls)
            {
                if (ctrl.Name.Contains("txt"))
                {
                    ctrl.Text = "";
                }

                if (ctrl.Name == "chkLaDangVien")
                {
                    chkLaDangVien.Checked = false;
                }
                if (ctrl.Name == "chklstChucVu")
                {
                    int numItem = chklstChucVu.Items.Count;
                    for (int i = 0; i < numItem; i++)
                    {
                        chklstChucVu.SetItemChecked(i, false);
                    }
                }

                if (ctrl.Name.Contains("cboNgay"))
                {
                    ctrl.Text = "--Ngày--";
                }
                if (ctrl.Name.Contains("cboThang"))
                {
                    ctrl.Text = "--Tháng--";
                }
            }
            cboDanToc.SelectedIndex = -1;
            cboGioiTinh.SelectedIndex = -1;
            cboChuyenMonNgVu.SelectedIndex = -1;
            cboLyLuanChinhTri.SelectedIndex = -1;
            cboTinhTrang.SelectedIndex = -1;
            cboToCongDoan.SelectedIndex = -1;
            cboTonGiao.SelectedIndex = -1;
            cboTrinhDoVanHoa.SelectedIndex = -1;
        }

        private void ResetControls()
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl.Name == "txtNhapTenCDV")
                {
                    ctrl.Text = "";
                }
                
                if (ctrl.Name == "cboChonToCongDoan")
                {
                    ctrl.Text = "";
                } 

                if (ctrl.Name == "grpThongTinCDV")
                {
                    ResetThongTinCDV();
                    ctrl.Enabled = false;
                }
                else
                {
                    ctrl.Enabled = true;
                }
            }
        }

        private void ThongTinCDV(CongDoanVienDTO CDVienDto, NgayChuyenDenDTO NgayCDenDto, NgayChuyenDiDTO NgayCDiDto)
        {
            CDVienDto.MaCDV = txtMaCDV.Text.Trim();
            CDVienDto.HoTen = txtHoTen.Text.Trim();
            CDVienDto.LaDangVien = chkLaDangVien.Checked;
            CDVienDto.SoDienThoai = txtSoDienThoai.Text.Trim();
            CDVienDto.DiaChi = txtDiaChi.Text.Trim();
            CDVienDto.SoTheDoanVien = this.txtSoTheDV.Text.Trim();
            CDVienDto.Ngach = Convert.ToInt32(txtNgach.Text.Trim());
            CDVienDto.HeSoLuong = (double)Convert.ToDouble(txtHeSoLuong.Text.Trim());

            if (cboGioiTinh.SelectedIndex != -1)
            {
                CDVienDto.GioiTinh = (string)cboGioiTinh.SelectedItem;
            }
            if (cboLyLuanChinhTri.SelectedIndex != -1)
            {
                CDVienDto.LyLuanChinhTri = (string)cboLyLuanChinhTri.SelectedItem;
            }
            if (cboTrinhDoVanHoa.SelectedIndex != -1)
            {
                CDVienDto.TrinhDoVanHoa = (string)cboTrinhDoVanHoa.SelectedItem;
            }
            if (cboChuyenMonNgVu.SelectedIndex != -1)
            {
                CDVienDto.ChuyenMonNghiepVu = (string)cboChuyenMonNgVu.SelectedItem;
            }
            if (cboTinhTrang.SelectedIndex != -1)
            {
                CDVienDto.TinhTrang = (string)cboTinhTrang.SelectedItem;
            }

            if (cboNgaySinh.SelectedIndex > 0 && cboThangSinh.SelectedIndex > 0 && txtNamSinh.Text.Trim() != "")
            {
                string ngaySinh = (string)cboThangSinh.SelectedItem + "/" + (string)cboNgaySinh.SelectedItem + "/" + txtNamSinh.Text.Trim();
                CDVienDto.NgaySinh = Convert.ToDateTime(ngaySinh);
            }

            if (cboNgayVaoCD.SelectedIndex != 0 && cboThangVaoCD.SelectedIndex != 0 && txtNamVaoCD.Text.Trim() != "")
            {
                string ngayVaoCD = (string)cboThangVaoCD.SelectedItem + "/" + (string)cboNgayVaoCD.SelectedItem + "/" + txtNamVaoCD.Text.Trim();
                CDVienDto.NgayVaoCongDoan = Convert.ToDateTime(ngayVaoCD);
            }

            if (cboToCongDoan.SelectedIndex != -1)
            {
                ToCongDoanDTO ToCDDto = new ToCongDoanDTO();
                ToCDDto = (ToCongDoanDTO)cboToCongDoan.SelectedItem;
                CDVienDto.ToCongDoan = ToCDDto.MaToCD;
            }
            if (cboDanToc.SelectedIndex != -1)
            {
                DanTocDTO DanTocDto = new DanTocDTO();
                DanTocDto = (DanTocDTO)cboDanToc.SelectedItem;
                CDVienDto.DanToc = DanTocDto.MaDanToc;
            }

            if (cboTonGiao.SelectedIndex != -1)
            {
                TonGiaoDTO TonGiaoDto = new TonGiaoDTO();
                TonGiaoDto = (TonGiaoDTO)cboTonGiao.SelectedItem;
                CDVienDto.TonGiao = TonGiaoDto.MaTonGiao;
            }

            foreach (int chucvu in chklstChucVu.CheckedIndices)
            {
                ChucVuDTO cvDto = new ChucVuDTO();
                cvDto.MaChucVu = chucvu + 1;
                lstChucVu.Add(cvDto);
            }
            CDVienDto.ChucVu = lstChucVu;


            if (cboNgayCDen.SelectedIndex > 0 && cboThangCDen.SelectedIndex > 0 && txtNamCDen.Text.Trim() != "")
            {
                NgayCDenDto.MaCDV = CDVienDto.MaCDV;

                string ngayChuyenDen = (string)cboThangCDen.SelectedItem + "/" + (string)cboNgayCDen.SelectedItem + "/" + txtNamCDen.Text.Trim();
                NgayCDenDto.NgayChuyenDen = Convert.ToDateTime(ngayChuyenDen);

                NgayCDenDto.LyDoChuyenDen = txtLyDoChuyenDen.Text.Trim();
            }


            if (cboNgayCDi.SelectedIndex > 0 && cboThangCDi.SelectedIndex > 0 && txtNamCDi.Text.Trim() != "")
            {
                NgayCDiDto.MaCDV = CDVienDto.MaCDV;

                string ngayChuyenDi = (string)cboThangCDi.SelectedItem + "/" + (string)cboNgayCDi.SelectedItem + "/" + txtNamCDi.Text.Trim();
                NgayCDiDto.NgayChuyenDi = Convert.ToDateTime(ngayChuyenDi);

                NgayCDiDto.LyDoChuyenDi = txtLyDoChuyenDi.Text.Trim();
            }

        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            string kt = KiemTraHopLe();
            if (kt != "")
            {
                MessageBox.Show(kt,"Thông báo");
                return;
            }
            CongDoanVienDTO CDVienDto = new CongDoanVienDTO();
            NgayChuyenDenDTO NgayCDenDto = new NgayChuyenDenDTO();
            NgayChuyenDiDTO NgayCDiDto = new NgayChuyenDiDTO();

            ThongTinCDV(CDVienDto, NgayCDenDto, NgayCDiDto);

            try
            {
                CongDoanVienBUS.Sua(CDVienDto, NgayCDenDto, NgayCDiDto, blnChucVu, blnNgayCDen, blnNgayCDi);
                MessageBox.Show("Đã cập nhật thông tin công đoàn viên");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            lstChucVu.Clear();
            ResetControls();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            ResetControls();
        }

        private void btnThemChucVu_Click(object sender, EventArgs e)
        {
            Form themChucVu = new frmThemChucVu();
            themChucVu.ShowDialog();

            ChucVuDTO cvDto = new ChucVuDTO();
            cvDto.TenChucVu = ((frmThemChucVu)themChucVu).ChucVu;
            if (cvDto.TenChucVu != "")
            {
                chklstChucVu.Items.Add(cvDto.TenChucVu);
                ChucVuBUS.Them(cvDto);
            }
        }
    }
}
